import { DICT_TYPE, getIntDictOptions } from "@/utils/dict";
import { Col, Form, Input, Modal, Row, Radio, Spin, } from "antd";
import React, { useState, useEffect, useImperativeHandle, } from "react";
import { dictApi } from "@/api";
const { TextArea } = Input;
/**@description 字典编辑新建表单 */
const DictForm = ({ onRef, getList = Function }) => {
    const [editType, setEditType] = useState("add");
    const [show, setShow] = useState(false);
    const [loading, setLoading] = useState(false);
    const [form] = Form.useForm();

    /**@description 表单布局 */
    const formItemLayout = {
        labelCol: {
            xs: {
                span: 24,
            },
            sm: {
                span: 4,
            },
        },
        wrapperCol: {
            xs: {
                span: 24,
            },
            sm: {
                span: 20,
            },
        },
    };

    /**@description 取消 */
    const onCanle = () => {
        setShow(false);
    }

    /**@description 添加|修改操作 */
    const open = async ({
        type = String || undefined,
        record = Object || {}
    }) => {
        console.log("字典修改编辑类 ：", type)
        setEditType(type);
        setShow(true);
        form.resetFields();
        form.setFieldValue("type", "action." + type)

        if (type === "update") {
            console.log("更新字典",record)
            const { dictNo, dictName, dictType, status, remark } = record;
            form.setFieldsValue({ dictNo, dictName, dictType, status, remark });
        }

    }

    /**@description 异步保存数据 */
    const save = async (value) => {
        const data = await dictApi.createDict(value);
        getList();
        onCanle();
    }

    /**@description 校验表单 */
    const validateForm = async () => {
        form.validateFields().then(res => {
            console.log("校验参数：", res)
            save(res)
        })
    }

    /**@description 向外暴露方法 */
    useImperativeHandle(onRef, () => ({
        open
    }))

    /**@description form表单props配置 */
    const formProps = {
        form,
    }

    useEffect(() => {
        return () => {
            form.resetFields();
        }
    }, [])


    return (
        <>
            <Modal
                title="编辑"
                width={550}
                open={show}
                centered={true}
                destroyOnClose={true}
                onCancel={onCanle}
                onOk={validateForm}
            >
                <Spin spinning={loading}>
                    <Form
                        {...formItemLayout}
                        {...formProps}
                    >
                        <Row gutter={24}>
                            <Col span={24} style={{ display: "none" }}>
                                <Form.Item label="字典编号" name="dictNo">
                                    <Input placeholder="请输入字典编号" />
                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item label="字典名称" name="dictName">
                                    <Input placeholder="请输入字典名称" />
                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item label="字典类型" name="dictType">
                                    <Input disabled={editType === "update" ? true : false} placeholder="请输入字典类型" />
                                </Form.Item>
                            </Col>

                            <Col span={24}>
                                <Form.Item label="字典状态" name="status">
                                    <Radio.Group
                                        options={getIntDictOptions(DICT_TYPE.COMMON_STATUS)}

                                    // optionType="button"
                                    // buttonStyle="solid"
                                    />
                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item label="备注" name="remark">
                                    <TextArea />
                                </Form.Item>
                            </Col>
                        </Row>
                    </Form>
                </Spin>

            </Modal>
        </>
    )
}
export default DictForm;